package com.kaifamiao.thread.d0928;


import java.io.*;

public class Demo {

    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        String source = "D:\\videos\\20220725PM.mp4";
        String directory = "d:\\cutter3";
        int size = 1024 * 1024 * 20; // 20M MB

        System.out.println(cutterWithThread(source, size, directory));

        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }


    public static int cutter(String source, int size, String directory){
        int count = 0;
        File file = new File(source);
        // 是否是文件
        if (file.isFile() && size > 0){
            // 读取文件
            try(FileInputStream fis = new FileInputStream(file)){
                byte[] bytes = new byte[size];

                int len = -1;

                File parent = new File(directory);
                if (!parent.exists()){
                    parent.mkdirs();
                }


                while ((len = fis.read(bytes)) != -1){
                    File out = new File(directory, count ++ + ".temp");

                    FileOutputStream fileOutputStream = new FileOutputStream(out);

                    fileOutputStream.write(bytes, 0, len);

                    fileOutputStream.close();
                }

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return count;
    }


    public static int cutterWithThread(String source, int size, String directory){
        int count = 0;
        File file = new File(source);
        // 是否是文件
        if (file.isFile() && size > 0){

            // 输出的目录是否存在
            File parent = new File(directory);
            if (!parent.exists()){
                parent.mkdirs();
            }


            int start = 0;
            int end = size;
            long fileSize = file.length();

            while( fileSize >= 0){
                File out = new File(directory, count ++ + ".temp");
                newCutterThread(source, out, start, size);
                start = end + 1;
                end = end + size;
                fileSize -= size;
            }
        }
        return count;
    }

    private static void newCutterThread(String source, File out, int start, int size){
        new Thread(() -> {
            try (   FileInputStream in = new FileInputStream(source);
                    FileOutputStream fos = new FileOutputStream(out);
                 ){
                System.out.println(start + "\t" + size);
                if (start != 0) {
                    in.skip(start - 1);
                }

                byte[] bytes = new byte[size];
                int len = -1;
                if ( (len = in.read(bytes)) != -1){
                    fos.write(bytes, 0, len);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();

    }
}
